function song(song_id)
{
	try
	{								
		$.post(webroot + "songs/save_play/"+song_id,
			function(message) {	
				if(message.status == 'ok') {
					//// alert(message.status);
					////window.location = webroot + 'songs/download/'+song_id;
				}
				else{
					//// alert(message.status);
				}
			},"json");
	}
	catch(e)
	{
	}
}

//download song if is_share = true
function download_song(song_id)
{
	window.location = webroot + 'songs/download/'+song_id;

	return false;
}

// SHARE SONG
function song_share(song_id, artist_id)
{
	picture_aftervote = server+"/img/artists/photo"+artist_id+".jpg";
	
	FB.ui(
		  {
			method: 'stream.publish', //'stream.publish',
			display: display_mode,
			picture : picture_aftervote + "?r=12345",
			name: song_name,
			caption: song_caption,
			description: song_description,
			message: song_message,
			link: server+"/songs/lyric/"+song_id,
			action_links: [
			  { text: action_links_text, href: action_links_href }
			]
		  }
		  
		   , function(response) {
 			if (response && response.post_id) 
			//if(1)
			{
			
				try
				{								
					$.post(webroot + "songs/save_share/"+song_id+"/0",
						   function(message) {	
							
						   },"json");
				}
				catch(e)
				{
				}
				$('#download_ok').replaceWith('<a id="download_ok" href="#" onclick="download_song('+song_id+');return false;"><img src="'+webroot+'img/downloadOK.gif" style="vertical-align:middle; margin-left:10px;" /></a>')
			  
			} else {
			  //// alert('Post was not published.');
			} 
		  } 
		  
	); 
}
		

//SHARE SONG AND THEN DOWNLOAD
function login_download(song_id, artist_id)
{
	picture_aftervote = server+"/img/artists/photo"+artist_id+".jpg";
				
	$.post(webroot + 'songs/check_share',
               function(message) {	
			   						// alert("after check share");
                   if(message.token == 0) {
						// alert("user have not shared");
							FB.ui(
								  {
									method: 'stream.publish', //'stream.publish',
									display: display_mode,
									picture : picture_aftervote + "?r=12345",
									name: song_name,
									caption: song_caption,
									description: song_description,
									message: song_message,
									link: server+"/songs/lyric/"+song_id,
									action_links: [
									  { text: action_links_text, href: action_links_href }
									]
								  }
								  
								   , function(response) {
									if (response && response.post_id) 
									//if(1)
									{
										try
										{								
											$.post(webroot + "songs/save_share/"+song_id+"/0",
												   function(message) {	
													   if(message.status == 'ok') {
															//// alert(message.status);
															window.location = webroot + 'songs/download/'+song_id;
													   }
													   else{
															//// alert(message.status);
													   }
												   },"json");
											
											$('#share_tip_popup').hide();											
											$('#download_ok').replaceWith('<a id="download_ok" href="#" onclick="download_song('+song_id+');return false;"><img src="'+webroot+'img/downloadOK.gif" style="vertical-align:middle; margin-left:10px;" /></a>')
										}
										catch(e)
										{
										}
									  
									} else {
									  //// alert('Post was not published.');
									}
								  }   
							); 	
                   }
				   else{
						//// alert("download, already shared");
						window.location = webroot + 'songs/download/'+song_id;
				   }
               },"json");	
	return false; 
}

function non_login_download(song_id, artist_id)
{
	picture_aftervote = server+"/img/artists/photo"+artist_id+".jpg";
	
			FB.ui(
				  {
					method: 'stream.publish', //'stream.publish',
					display: display_mode,
					picture : picture_aftervote + "?r=12345",
					name: song_name,
					caption: song_caption,
					description: song_description,
					message: song_message,
					link: server+"/songs/lyric/"+song_id,
					action_links: [
					  { text: action_links_text, href: action_links_href }
					]
				  }
				  
				   , function(response) {
					if (response && response.post_id) {
					  //// alert('Post was published.');
					  
						try
						{								
							$.post(webroot + "songs/save_share/"+song_id+"/0",
								   function(message) {	
									   if(message.status == 'ok') {
											// alert(message.status);
											window.location = webroot + 'songs/download/'+song_id;
									   }
									   else{
											// alert(message.status);
									   }
								   },"json");
							
							$('#share_tip_popup').hide();
							$('#download_ok').replaceWith('<a id="download_ok" href="#" onclick="download_song('+song_id+');return false;"><img src="'+webroot+'img/downloadOK.gif" style="vertical-align:middle; margin-left:10px;" /></a>')
						}
						catch(e)
						{
						}
					  
					} 
					else 
					{
					  // alert('Post was not published.');
					}
				  } 
				  
			); 
                   
	
	return false;
}


function invite()
{
	FB.ui({method: 'apprequests'
		 , message: 'Invite your friends to choose their favourite privileges now!'
		 , filters: ['app_non_users']
		 , data: 'tracking information for the user'});
}

function term(){
	jQuery.facebox({ ajax: webroot + 'pledges/term'});
}

// VALIDATE REGISTRATION FORM
function validate_registration()
{
	//validate and process form here
	$('.error').hide();
	
	//email address as username
	var UserUsername = $("input#UserUsername").val();
	if (UserUsername == "") 
	{
		$("p#emailusername_empty").show();
		$("input#UserUsername").focus();
		return false;
	}
	else if(!email_validate())
	{
		$("p#emailusername_format").show();
		$("input#UserUsername").focus();
		return false;
	}
	else if(!check_username())
	{
		$('.error').hide();
		$("p#emailusername_repeat").show();
		$("input#UserUsername").focus();
		return false;
	}


			
	//password 1
	var UserPassword = $("input#UserPassword").val();
	if (UserPassword == "") {
		$("p#password").show();
		$("input#UserPassword").focus();
		return false;
	}
	else if(UserPassword.length < 6)
	{
		$("p#password_length").show();
		$("input#UserPassword").focus();
		return false;
	}
	
	//password confirm
	var UserPasswordConfirm = $("input#UserPasswordConfirm").val();
	if (UserPasswordConfirm == "") {
		$("p#password_confirm").show();
		$("input#UserPasswordConfirm").focus();
		return false;
	}
	
	if(!(UserPassword == UserPasswordConfirm))
	{
		$("p#password_notmatched").show();
		$("input#UserPasswordConfirm").focus();
		return false;
	}
	
	return true;  	

}

function validate_profile()
{
	//validate and process form here
	$('.error').hide();
	
	//first name
	var UserFirstName = $("input#UserFirstName").val();
	if (UserFirstName == "" || UserFirstName == "First Name") {
		$("p#name").show();
		$("input#UserFirstName").focus();
		return false;
	}
	
	//last name
	var UserLastName = $("input#UserLastName").val();
	if (UserLastName == "" || UserLastName == "Last Name" ) {
		$("p#name").show();
		$("input#UserLastName").focus();
		return false;
	}
	
	//gender
	if (!($("input#UserGenderMale:checked").val() == 'male' || $("input#UserGenderFemale:checked").val() == 'female')) {
		$("p#isgender").show();
		$("input#UserGenderMale").focus();
		$("input#UserGenderFemale").focus();
		return false;
	}

	//DOB month
	var UserMonth = $("select#UserMonth").val();
	if (UserMonth == "Month") {
		$("p#birthday").show();
		$("select#UserMonth").focus();
		return false;
	}
	//DOB day
	var UserDay = $("select#UserDay").val();
	if (UserDay == "Day") {
		$("p#birthday").show();
		$("select#UserDay").focus();
		return false;
	}
	//DOB year
	var UserYear = $("select#UserYear").val();
	if (UserYear == "Year") {
		$("p#birthday").show();
		$("select#UserYear").focus();
		return false;
	}
	
	//location
	var UserLocation = $("select#UserLocation").val();
	if (UserLocation == "") {
		$("p#location").show();
		$("select#UserLocation").focus();
		return false;
	}
	
/* 	//phone
	var phone = $("input#UserPhone").val();
	if (phone == "") {
		$("p#contactnum").show();
		$("input#UserPhone").focus();
		return false;
	} */
	
	return true;
}

//RESET PASSWORD VALIDATION
function validate_resetpassword()
{
	//validate and process form here
	$('.error').hide();
	
	//first name
	var UserUsername = $("input#UserUsername").val();
	if (UserUsername == "") {
		$("p#name").show();
		$("input#UserUsername").focus();
		return false;
	}
	
	return true;
}

//EMAIL VALIDATION
function email_validate() {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = $('#UserUsername').val();
   if(reg.test(address) == false) {
      //alert('Invalid Email Address');
      return false;
   }
   else
   {
		return true;
   }
}

function check_availability()
{
	$('.error').hide();
	$("span#username_available").hide();
	
	//email address as username
	var UserUsername = $("input#UserUsername").val();
	if (UserUsername == "") 
	{
		$("p#emailusername_empty").show();
		$("input#UserUsername").focus();
		return false;
	}
	else if(!email_validate())
	{
		$("p#emailusername_format").show();
		$("input#UserUsername").focus();
		return false;
	}
	else if(!check_username())
	{
		$("p#emailusername_repeat").show();
		$("input#UserUsername").focus();
		return false;
	}
	
	$('.error').hide();
	$("span#username_available").show();
	$("input#UserPassword").focus();
	
	return true;
	
}

// CHECK DUPLICATE USERNAME
function check_username()
{
var available = false;

	$.ajax({
		type: "POST",
		async: false, // Very important, must wait for this call to be complete
		url: webroot + "users/username/"+$('#UserUsername').val(),
		success: function(message) {
				message = jQuery.parseJSON(message);
				//// alert(message.available);
				if(message.available)
				{
					//// alert('username available!');

					available = true;
				}
				else
				{
					//// alert('this username has already been taken!');

					available = false;
				}
			}
		}, "json");
		
	return available;
}
// REGISTER SUBMIT
function submit_register()
{
	if(!validate_registration())
	{
		return false;
	}

	
	
	var dataString = $("form").serialize();
	
	$.ajax({
	type: "POST",
	async: false,
	url: webroot + "users/register",
	data: dataString,
	success: function(message) {
			message = jQuery.parseJSON(message);
			if(message.status == 'ok')
			{
				window.location = webroot+"users/profile/"+message.id;
			}
			else
			{
				// alert("Unable to register at this moment. Please try again later");
			}

		}
	}, "json");
	
	// return false;
}

// UPDATE PARTICULAR
function submit_profile()
{
	if(!validate_profile()) return false;
	
	var dataString = $("form").serialize();
	
	$.ajax({
	type: "POST",
	async: false,
	url: webroot + "users/profile",
	data: dataString,
	success: function(message) {
			message = jQuery.parseJSON(message);
			if(message.status != 'ok')
			{
				// alert("Unable to save your profile at this moment. Please try again later");
			}
			
			// redirect user anyway
			window.location = webroot;
		}
	}, "json");
	
	// return false;
}

function submit_resetpassword()
{
	if(!validate_resetpassword()) return false;
	
	var dataString = $("form").serialize();
	
	$.ajax({
	type: "POST",
	async: false,
	url: webroot + "users/resetpassword",
	data: dataString,
	success: function(message) {
			message = jQuery.parseJSON(message);
			if(message.status != 'ok')
			{
				// alert("Unable to save your profile at this moment. Please try again later");
			}
			
			// redirect user anyway
			window.location = webroot;
		}
	}, "json");
	
	// return false;
}

function detail_demographic(id){
	jQuery.facebox({ div: '#'+id });
}

function detail_psychographic(id){
	jQuery.facebox({ div: '#'+id });
}

 $(document).ready(function(){
  $('.error').hide();
 }); 


